﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTerm.MsSql.DbProvider;

namespace eTerm.Platform.Web
{
    public partial class UserList : BasePage
    {
        protected override void InitPageLoad()
        {
            dlCompany.DataSource =new List<SysCompany>(){ new SysCompany(){ CompanyCode=string.Empty, CompayName =@"  全  部  " } }.Union( SysCompany.All().ToList());
            dlCompany.DataBind();
            QueryData();
        }


        protected void AspNetPager1_PageChanging(object sender, Wuqi.Webdiyer.PageChangingEventArgs e) {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            QueryData();
        }


        protected void rptDetr_OnItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                var dataItem = e.Item.DataItem;
                LinkButton lnkEdit = e.Item.FindControl(@"lnkEdit") as LinkButton;


                BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, lnkEdit.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-save'  
}});  
", lnkEdit.ClientID), true);

                //,Get<object>(dataItem,@"UserModel.UserId")
                lnkEdit.OnClientClick = string.Format(@" 
$.jBox(""iframe:UserEdit.aspx?ID={0}"", {{
    title: ""用户信息"",
    width: 800,
    height:450,
    buttons: {{ }},
    closed: function () {{ 
        $('#{1}').click();
    }} /* 窗口关闭后执行的函数 */
}});
return false;", Get<object>(dataItem, @"UserModel.UserId"),btnQuery.ClientID);
            }
        }

        protected void QueryData() {
            var sql = SysUser.All().Join(SysCompany.All(),l=>l.CompanyCode,r=>r.CompanyCode,(l,r)=>new { UserModel = l,CompanyName = r.CompayName }) ;

            if (!string.IsNullOrEmpty(dlCompany.SelectedValue))
                sql = sql.Where(l => l.UserModel.CompanyCode == dlCompany.SelectedValue);
            if (!string.IsNullOrEmpty(tbUserNo.Text.Trim()))
                sql = sql.Where(l => l.UserModel.UserNo.Contains(tbUserNo.Text.Trim()));

            if (!string.IsNullOrEmpty(tbDisplayName.Text.Trim()))
                sql = sql.Where(l => l.UserModel.DisplayName.Contains(tbDisplayName.Text.Trim()));
            AspNetPager1.RecordCount = sql.Count();
            this.rptDetr.DataSource = sql.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize)
                .OrderBy(l => l.UserModel.UserId)
                .Select(l => new { l.UserModel,l.CompanyName });
                ;
            this.rptDetr.DataBind();
            this.AspNetPager1.CustomInfoHTML = string.Format("当前第{0}/{1}页 共{2}条记录 每页{3}条", new object[] { this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });
        }

        protected override void InitEasyUI()
        {
            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnQuery.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-search'  
}});  
", btnQuery.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, btnNew.ClientID, string.Format(@"$('#{0}').linkbutton({{   
    iconCls: 'icon-add'  
}});  
", btnNew.ClientID), true);

            BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, dlCompany.ClientID, string.Format(@"$('#{0}').combobox();  
", dlCompany.ClientID), true);

            //BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, tbDisplayName.ClientID, string.Format(@"$('#{0}').searchbox({{ prompt:'请输入姓名' }}); ", tbDisplayName.ClientID), true);
            //BaseRegisterStartupScript<UpdatePanel>(GetUpdatePanel(@"pnlContent") as UpdatePanel, tbUserNo.ClientID, string.Format(@"$('#{0}').searchbox({{ prompt:'请输入登录名' }}); ", tbUserNo.ClientID), true);


            //,Get<object>(dataItem,@"UserModel.UserId")
            btnNew.OnClientClick = string.Format(@" 
$.jBox(""iframe:UserEdit.aspx?ID={0}"", {{
    title: ""用户信息"",
    width: 800,
    height:450,
    buttons: {{ }},
    closed: function () {{ 
        $('#{1}').click();
    }} /* 窗口关闭后执行的函数 */
}});
return false;", 0,btnQuery.ClientID);
        }

        protected override string PageCode
        {
            get { return @"USERS"; }
        }

        protected void btnQuery_Click(object sender, EventArgs e) {
            AspNetPager1.CurrentPageIndex = 1;
            QueryData();

        }
    }
}
